package com.hfi.ood.payroll.schedule;

import com.hfi.ood.payroll.domain.PaymentSchedule;
import lombok.extern.slf4j.Slf4j;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

import static java.time.temporal.ChronoUnit.DAYS;

/**
 * @author ChangLiang
 * @date 2020/5/19
 */
@Slf4j
public class BiweeklySchedule implements PaymentSchedule {

    private Date firstPayableFriday = Date.from(LocalDate.of(2020, 5, 15).atStartOfDay(ZoneId.systemDefault()).toInstant());

    @Override
    public boolean isPayDay(Date date) {
        // 每隔一周的周五
        LocalDate firstFriday = Instant.ofEpochMilli(firstPayableFriday.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate current = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
        long between = DAYS.between(firstFriday, current);
        return between % 14 == 0 ? true : false;
    }

    @Override
    public Date getPayPeriodStartDate(Date payPeriodEndDate) {
        LocalDate current = Instant.ofEpochMilli(payPeriodEndDate.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
        return Date.from(current.plus(-14, DAYS).atStartOfDay(ZoneId.systemDefault()).toInstant());
    }
}
